<template>
    <contentfield v-if="!$store.state.user.pulling_info"> <!-- 使用Grid系统 --> 
        <div class="row justify-content-md-center"> <!-- 居中 -->
            <div class="col-3">  <!-- 十二列占三列 -->
                <form @submit.prevent="login">
                    <div class="mb-3">
                        <label for="username" class="form-label">用户名</label>
                        <input v-model="username" type="text"  class="form-control" id="username" placeholder="请输入用户名">
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">密码</label>
                        <input v-model="password" type="password" class="form-control" id="password" placeholder="请输入密码">
                    </div>
                    <div class="error-message">{{ error_message }}</div>
                    <button type="submit" class="btn btn-primary">登录</button>
                </form>
            </div>
        </div>
    </contentfield>
</template>
<script>
import ContentField from "@/components/ContentField.vue"
import { useStore } from "vuex";
import router from '@/router';
import {ref} from "vue";
export default {
    components:{
        contentfield:ContentField,
    },
    setup(){
        const store = useStore();//取出全局变量
        let username = ref("");//定义username 初始为空
        let password = ref("");//定义password 
        let error_message = ref(" ");//提示用户错误信息
        const jwt_token = localStorage.getItem("jwt_token");
        if(jwt_token){//若jwt_token存在
            store.commit("updateToken",jwt_token);
            //判断jwt_token是否有效
            store.dispatch("getinfo",{
                success(){
                    router.push({name:"home"});
                    store.commit("updatePullingInfo",false);//拉取结束
                },
                error(){
                    store.commit("updatePullingInfo",false);//拉取结束
                }
            })

        }else{ 
            store.commit("updatePullingInfo",false);
        }

        const login = ()=>{ //定义login函数，当页面提交时触发
            error_message.value = "";
            store.dispatch("login",{ //使用dispath来调用store/user.js中action中的函数
                username:username.value, //ref变量取值使用value
                password:password.value,
                success(){
                    store.dispatch("getinfo",{
                        success(){
                            router.push({name:"home"});//如果登录成功，跳转到home页面
                            //console.log(store.state.user);
                        }
                    })
                    
                },
                error(resp){ //如果登录失败，给用户个提示
                    console.log(resp);
                    error_message.value="用户名或者密码错误";

                }


            })

        }
        return{
        username,
        password,
        error_message,
        login
        }
    }
    
}
</script>
<style scoped>
button{
    width:100%;
}
.error-message{
    color:red
}
</style>